<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" title="登录" />
    <!-- /导航栏 -->

    <!-- 登录表单 -->
    <van-form @submit="onSubmit">
      <van-field v-model="user.mobile" name="手机号" placeholder="请输入手机号">
        <!-- 使用slot插槽插入自定义图标 -->
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="user.code" name="验证码" placeholder="请输入验证码">
        <template #button>
          <van-button class="send-sms-btn" round size="small" type="default"
            >获取验证码</van-button
          >
        </template>
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
    <!-- /登录表单 -->
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'LoginIndex',
  components: {},
  props: {},
  data () {
    return {
      user: {
        mobile: '', // 手机号
        code: '' // 验证码
      }
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onSubmit () {
      // 1获取表单数据
      //   const user = this.user
      // 2 表单验证
      // 3 提交表单 请求登录
      try {
        const res = await login(this.user)
        console.log('登录成功', res)
      } catch (err) {
        if (err.response.status === 400) {
          console.log('手机号或验证码错误', err)
        } else {
          console.log('登录失败，请稍候重试', err)
        }
      }
      // 4 根据请求处理后续操作
    }
  }
}
</script>
<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 24px;
  }
  .send-sms-btn {
    background-color: #ededed;
    width: 100px;
    height: 28px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 36px 16px 0;

    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
